﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Threading;
using QXGameEngine.Interface;
using QXGameEngine.Logic;

namespace QXGameEngine.Control {

    public partial class QXDecoration : UserControl, IObject {

        /// <summary>
        /// 装饰类控件
        /// </summary>
        public QXDecoration() {
            InitializeComponent();
            Timer = new DispatcherTimer();
            Timer.Interval = TimeSpan.FromMilliseconds(100);
            Timer.Tick += new EventHandler(Timer_Tick);
            Timer.Start();
        }
     
        //从父窗口中移除并且回收
        public void  RemoveFromCanvas()
        {
            Timer.Stop();
            this.ParentCanvas.Children.Remove(this);
            this.FrameCounter = this.StartFrame;
            Globle.CursorList[this.Code.ToString()].Enqueue(this);

        }
        /// <summary>
        /// 获取或设置内置计时器
        /// </summary>
        public DispatcherTimer Timer { get; set; }

        /// <summary>
        /// 获取或设置根画布容器
        /// </summary>
        public Canvas RootCanvas {
            get { return this.Container; }
        }
        public string Name { set; get; }
        /// <summary>
        /// 获取父容器
        /// </summary>
        public Canvas ParentCanvas {
            get { return this.Parent as Canvas; }
        }

        /// <summary>
        /// 获取或设置装饰特性类型
        /// </summary>
        public DecorationTypes DecorationType { get; set; }

        /// <summary>
        /// 获取或设置代号
        /// </summary>
        public int Code { get; set; }

        /// <summary>
        /// 获取或设置帧推进器
        /// </summary>
        public int FrameCounter { get; set; }

        /// <summary>
        /// 获取或设置物体当前开始图片列号
        /// </summary>
        public int StartFrame { get; set; }

        /// <summary>
        /// 获取或设置物体当前结束图片列号
        /// </summary>
        public int EndFrame { get; set; }

        /// <summary>
        /// 获取或设置控件主体宽
        /// </summary>
        public double BodyWidth {
            get { return Container.Width; }
            set { Container.Width = value; }
        }

        /// <summary>
        /// 获取或设置控件主体高
        /// </summary>
        public double BodyHeight {
            get { return Container.Height; }
            set { Container.Height = value; }
        }

        /// <summary>
        /// 获取或设置物体中心点距离左边X坐标
        /// </summary>
        public double CenterX { get; set; }

        /// <summary>
        /// 获取或设置物体中心点距离顶边Y坐标
        /// </summary>
        public double CenterY { get; set; }
   
        /// <summary>
        /// 获取或设置坐标(关联属性)
        /// </summary>
        
        public Point Coordinate {
            get { return (Point)GetValue(CoordinateProperty); }
            set { SetValue(CoordinateProperty, value); }
        }
        public static readonly DependencyProperty CoordinateProperty = DependencyProperty.Register(
            "Coordinate",
            typeof(Point),
            typeof(QXDecoration),
            new PropertyMetadata(ChangeCoordinateProperty)
        );
        private static void ChangeCoordinateProperty(DependencyObject d, DependencyPropertyChangedEventArgs e) {
            QXDecoration obj = (QXDecoration)d;
            if (obj.Visibility == Visibility.Visible) {
                Point p = (Point)e.NewValue;
                obj.SetValue(Canvas.LeftProperty, p.X - obj.CenterX);
                obj.SetValue(Canvas.TopProperty, p.Y - obj.CenterY);
                obj.SetValue(Canvas.ZIndexProperty, (int)p.Y);
            }
        }

        /// <summary>
        /// 计时器事件
        /// </summary>
        public void Timer_Tick(object sender, EventArgs e) {
            Container.Background = new ImageBrush() { ImageSource = Super.GetImage(string.Format(@"/Image/Item/{0}/{1}.png", Code, FrameCounter)) };
            if (FrameCounter == EndFrame) {
                switch (DecorationType) {
                    case DecorationTypes.Loop:
                        FrameCounter = 0;
                        break;
                    case DecorationTypes.AutoRemove:
                        if (Name == "Cursor")
                        {
                            RemoveFromCanvas();
                        }
                        else
                        {
                            Super.RemoveObject(this, true);
                        }
                        break;
                    default:
                        Timer.Stop();
                        this.Visibility = Visibility.Collapsed;
                        break;
                }
            } else {
                FrameCounter += 1;
            }
        }
    }
}
